import axios from 'axios'//引入axios
import router from "./router/index.js";
import {Notify} from "vant"

//request 请求拦截
axios.interceptors.request.use(config => {
    if (localStorage.eleToken) //判断localStorage中的token是否存在 存在的话给每条请求加上请求头信息
        config.headers.Authorization = localStorage.eleToken;  //设置协议
    return config;

}, err => {
    //返回的错误会被请请求的catch捕获
    return Promise.reject(err);
});

// response 拦截器
axios.interceptors.response.use(response => {
    return response
}, error => {
    const {status} = error.response;
    if (status === 401) {
        Notify({type: 'warning', message: '请登录'});
        // 清除token
        localStorage.removeItem('eleToken');
        // 页面跳转
        router.replace("/login");
    }
    return Promise.reject(error);
});
export default axios;